<?php 
namespace Admin\Model;;

/**
 * 商品服务类
 */
class GoodsModel extends BaseModel {
	/**
	 * 分页列表[获取已审核列表]
	 */
     public function queryByPage(){
        $m = M('goods');
        $shopName = I('shopName');
     	$goodsName = I('goodsName');
     	$areaId1 = (int)I('areaId1',0);
     	$areaId2 = (int)I('areaId2',0);
     	$goodsCatId1 = (int)I('goodsCatId1',0);
     	$goodsCatId2 = (int)I('goodsCatId2',0);
     	$goodsCatId3 = (int)I('goodsCatId3',0);
     	$isAdminBest = (int)I('isAdminBest',-1);
     	$isAdminRecom = (int)I('isAdminRecom',-1);
	 	$sql = "select g.*,gc.catName,sc.catName shopCatName,p.shopName from __PREFIX__goods g 
	 	      left join __PREFIX__goods_cats gc on g.goodsCatId3=gc.catId 
	 	      left join __PREFIX__shops_cats sc on sc.catId=g.shopCatId2,__PREFIX__shops p 
	 	      where goodsStatus=1 and goodsFlag=1 and p.shopId=g.shopId and g.isSale=1";
	 	if($areaId1>0)$sql.=" and p.areaId1=".$areaId1;
	 	if($areaId2>0)$sql.=" and p.areaId2=".$areaId2;
	 	if($goodsCatId1>0)$sql.=" and g.goodsCatId1=".$goodsCatId1;
	 	if($goodsCatId2>0)$sql.=" and g.goodsCatId2=".$goodsCatId2;
	 	if($goodsCatId3>0)$sql.=" and g.goodsCatId3=".$goodsCatId3;
	 	if($isAdminBest>=0)$sql.=" and g.isAdminBest=".$isAdminBest;
	 	if($isAdminRecom>=0)$sql.=" and g.isAdminRecom=".$isAdminRecom;
	 	if($shopName!='')$sql.=" and (p.shopName like '%".$shopName."%' or p.shopSn like '%".$shopName."%')";
	 	if($goodsName!='')$sql.=" and (g.goodsName like '%".$goodsName."%' or g.goodsSn like '%".$goodsName."%')";
	 	$sql.="  order by goodsId desc";   
		return $m->pageQuery($sql);
	 }

	 /**
	  * 获取商品信息
	  */
	 public function get() {
	 	$m = M('goods');
	 	$id = (int)I('id',0);
	 	$goods = $m->where('goodsId='.$id)->find();
	 	
	 	$sql = "select c1.catName goodsName1,c2.catName goodsName2,c3.catName goodsName3
		        from __PREFIX__goods_cats c3 , __PREFIX__goods_cats c2,__PREFIX__goods_cats c1
		        where c3.parentId=c2.catId and c2.parentId=c1.catId and c3.catId=".$goods['goodscatid3'];
	 	$rs = $this->query($sql);
	 	$goods['goodscats']=$rs[0];
	 	//店铺分类
		$sql = "select c1.catName goodsName1,c2.catName goodsName2
		        from __PREFIX__shops_cats c2 ,__PREFIX__shops_cats c1
		        where c2.parentId=c1.catId and c2.catId=".$goods['shopcatid2'];
		$rs = $this->query($sql);
		$goods['shopcats'] = $rs[0];
		//属性
		if($goods['attrcatid']>0){
			$sql = "select catName from __PREFIX__attribute_cats where catId=".$goods['attrcatid'];
			$rs = $this->query($sql);
		    $goods['attrcatname'] = $rs[0]['catname'];

			//获取规格属性
			$sql = "select ga.attrVal,ga.attrPrice,ga.attrStock,a.attrId,a.attrName,a.isPriceAttr,ga.isRecomm
			            ,ga.isRecomm from __PREFIX__attributes a
			            left join __PREFIX__goods_attributes ga on ga.attrId=a.attrId and ga.goodsId=".$id." where
						a.attrFlag=1 and a.catId=".$goods['attrcatid']." and a.shopId=".$goods['shopid'];
			$attrRs = $m->query($sql);
			if(!empty($attrRs)){
				$priceAttr = array();
				$attrs = array();
				foreach ($attrRs as $key =>$v){
					if($v['ispriceattr']==1){
						$goods['priceattrname'] = $v['attrname'];
						$priceAttr[] = $v;
					}else{
						$v['attrcontent'] = $v['attrval'];
						$attrs[] = $v;
					}
				}
				$goods['priceattrs'] = $priceattr;
				$goods['attrs'] = $attrs;
			}
		}
	 	return $goods;
	 }









}



 ?>